package cn.kgc.controller;

import cn.kgc.pojo.Employee;
import cn.kgc.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈俊良
 * @version 1.0
 * @date 2022/10/7 - 14:19
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/employee")
    public String employee() {
        return "employee";
    }

    //查询所有

    @ResponseBody
    @RequestMapping("/findEmployeeListjson")
    public Map<String, Object> findEmployeeListjson(String page, String limit, String empName) {
        HashMap<String, Object> map = new HashMap<>();
        int crrPage = 1;
        if (page != null && !"".equals(page)) {
            crrPage = Integer.parseInt(page);
        }
        int i = Integer.parseInt(limit);
        crrPage = (crrPage - 1) * i;

        //查询所有数据
        List<Employee> list = employeeService.findEmployeeList(crrPage, i, empName);
        //得到总页数
        int totalPage = employeeService.getTotalCount(empName);
        map.put("count", totalPage);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        return map;
    }

    //新增
    @RequestMapping("/addEmployee")
    @ResponseBody
    public Map<String, Object> addEmployee(Employee employee) {
        HashMap<String, Object> map = new HashMap<>();
        int i = employeeService.addEmployee(employee);
        System.out.println(employee);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", i);
        return map;
    }

    //删除
    @RequestMapping("/deleteEmployee")
    public String deleteEmployee(String empId) {
        employeeService.deleteEmployee(empId);
        return "redirect:/employee";
    }

    //修改
    @RequestMapping("/updateEmployee")
    @ResponseBody
    public Map<String, Object> updateEmployee(Employee employee) {

        HashMap<String, Object> map = new HashMap<>();
        int i = employeeService.updateEmployee(employee);

        map.put("code", 0);
        map.put("msg", "");
        map.put("data", i);

        return map;
    }
    @RequestMapping("/queryForList")
    @ResponseBody
    public List<Map<String,Object>> queryForList(Integer empId){
        List<Map<String, Object>> mList = employeeService.queryForList(empId);

        return mList;
    }

}
